import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xvideos/hj_model/media_content.dart';
import 'package:xvideos/hj_utils/api_service/acg_service.dart';

import '../../hj_model/acg/comic_chapters_model.dart';
import '../../hj_model/cartoon_media_info.dart';
import '../../hj_utils/global.dart';
import '../../tools_base/loading/LoadingHelper.dart';
import 'widget/acg_source_manager.dart';

//文字小说阅读页
class TextNovelReadLogic extends GetxController {
  CartoonMediaInfo? mediaInfo;
  // String? subId; //子集id

  List<ComicChapterInfo> allEpisodes = [];

  MediaContent? detailInfo; //当前子集详情
  int currentEpisode = 0; //当前的子集数 ，从1开始自增

  bool showMenu = true;
  bool isDark = true;
  ScrollController scrollCtr = ScrollController();
  int episodeIndex = 1; //当前的子集数 ，从1开始自增
  RxBool isAscend = true.obs; //排序类型 0-正序 1-倒序
  final ACGSourceManager manager;
  int durationTime = 250;

  TextNovelReadLogic(this.manager)
      : mediaInfo = manager.mediaInfo,
        currentEpisode = manager.index;

  @override
  void onReady() {
    super.onReady();
    allEpisodes.addAll(manager.allEpisodes); //列表复制，方便反转
    loadData();
  }

  //获取文字小说
  loadData({bool showLoading = false, bool needToTop = false}) async {
    if (showLoading == true) LoadingHelper.showLoading();
    final model = await manager.getComicsMediaContent(index: currentEpisode);
    if (showLoading == true) LoadingHelper.dismissLoading();
    await manager.checkPermission(
      model,
      action: () => setData(model),
    );
    update();
    if (needToTop) {
      toTop();
    }
  }

  //回到顶部
  Future toTop() async {
    Future.delayed(Duration(milliseconds: 100)).then(
      (value) => scrollCtr.jumpTo(0),
    );
  }

  //true- 下一集 false- 上一集
  loadOtherEpisode(bool next, {bool showLoading = false}) async {
    int index = next ? currentEpisode + 1 : currentEpisode - 1;
    if (index < 0) {
      showToast('这是第一${mediaInfo?.unit}了喔～');
      return;
    }
    if (index > manager.allEpisodes.length - 1) {
      showToast('这是最后一${mediaInfo?.unit}了喔～');
      return;
    }
    final model = await manager.getComicsMediaContent(index: index);
    currentEpisode = index;
    setData(model);
    update();
    toTop();
  }

  loadDataWithIndex(int index) async {
    LoadingHelper.showLoading();
    MediaContent? model = await ACGService.getMediaDetailInfo(
      episodeNumber: index,
      mediaId: manager.mediaInfo?.id,
    );
    LoadingHelper.dismissLoading();
    if (model != null) {
      currentEpisode = index - 1;
      setData(model);
    }

    update();
  }

  setData(MediaContent? model) {
    if (model != null) {
      detailInfo = model;
      manager.changeEpisodesIndex((model.episodeNumber ?? 1) - 1);
      print('保存集数 === currentEpisode = $detailInfo, mediaInfo.id = ${mediaInfo?.id}');
      update();
    } else {
      detailInfo = MediaContent();
    }
  }

  //展示菜单
  onShowMenuAction() {
    showMenu = !showMenu;
    update();
  }

  //切换暗黑模式
  changeDarkStyle() {
    isDark = !isDark;
    update();
  }

  //排序
  onChangeSortAction() {
    isAscend.value = !isAscend.value;
    allEpisodes = allEpisodes.reversed.toList();
    update(['list']);
  }
}
